package leetcode.algorithm.linkedlist.simple.tree;

import leetcode.algorithm.TreeNode;

/**
 * 将有序数组变为有序二叉树
 * @author jiangwz
 * @create 2019/11/9.
 */
public class Q108 {

    public static void main(String[] args){

      int[] nums = new int[]{-10,-3,0,5,9};
      new Q108().sortedArrayToBST(nums);
    }

    public TreeNode sortedArrayToBST(int[] nums) {
        return sorted(nums, 0, nums.length);
    }

    public TreeNode sorted(int[] nums,int start, int end) {
        if(start == end){
            return null;
        }
        System.out.println((end + start) >> 1);
        int mid = (end + start) >>> 1;
        TreeNode root = new TreeNode(nums[mid]);
        root.left = sorted(nums, start, mid);
        root.right = sorted(nums, mid+1, end);
        return root;
    }
}
